<template>
  <div class="app-container">
    <div>
      <el-row class="admin-top-g">
        <GButton
          :is-add-show="!!(limits.length >=1 && limits.filter(it => it.FName === '新增').length)"
          :is-edit-show="!!(limits.length >=1 && limits.filter(it => it.FName === '修改').length)"
          :is-del-show="!!(limits.length >=1 && limits.filter(it => it.FName === '删除').length)"
          :lists="options"
          @onAdd="addUser"
          @onEdit="editUser"
          @onDelete="delUser"
          @selectSettings="selectSettings"
        />

      </el-row>
      <br>
      <el-row>

        <template v-if="limits.length >=1 && limits.filter(it => it.FName === '查看').length">
          <!-- <el-date-picker
            v-model="qjValue"
            style="width: 195px;"
            type="month"

            placeholder="选择年度"
            @change="selectPeriod"/> -->
          <el-input v-model="keyword" clearable style="width: 195px;" placeholder="请输入关键字" @keyup.native.enter="searchUser" @change="searchUser"/>
          <!-- <el-select v-model="fcompany" clearable filterable placeholder="请选择公司" @change="searchUser">
            <el-option
              v-for="item in gsList"
              :key="item.finterid"
              :label="item.fname"
              :value="item.fname"/>
          </el-select> -->
          <el-button type="primary" icon="el-icon-search" @click="searchUser">搜索</el-button>
        </template>

        <el-button v-if="limits.length >=1 && limits.filter(it => it.FName === '导出').length" type="success" icon="el-icon-download" @click="downData">导出</el-button>
        <!-- 上传文件 -->
        <!-- <el-upload
          v-if="limits.length >=1 && limits.filter(it => it.FName === '导入').length"
          ref="upload"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :file-list="fileList"
          :limit="1"
          :data="upData"
          :on-exceed="exceed"
          :on-success = "notice"
          :before-upload="beforeAvatarUpload"
          :auto-upload="false"
          style="display: inline-block; margin-left: 10px;"
          class="upload-demo"
          action="/excel/jsImport">
          <el-button slot="trigger" size="small" type="primary">上传xls文件</el-button>
          <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">提交</el-button>
        </el-upload> -->
        <!-- <el-select v-model="appUser" filterable clearable placeholder="请选择员工" @change="searchUser">
          <el-option
            v-for="item in appUserList"
            :key="item.FUserID"
            :label="item.FName"
            :value="item.FUserID"/>
        </el-select> -->
        <!-- <el-button v-if="limits.length >=1 && limits.filter(it => it.FName === '审核').length" type="success" icon="el-icon-circle-check" @click="check('1')">审核</el-button>
        <el-button v-if="limits.length >=1 && limits.filter(it => it.FName === '反审核').length" type="success" icon="el-icon-close" @click="check('0')">反审</el-button> -->

        <!-- 导入模板 -->
        <el-upload
          v-if="limits.length >=1 && limits.filter(it => it.FName === '导入').length"
          ref="uploadBasic"
          :on-preview="handlePreviewBasic"
          :on-remove="handleRemoveBasic"
          :file-list="fileListBasic"
          :limit="1"
          :data="upDataBasic"
          :on-exceed="exceedBasic"
          :on-success = "noticeBasic"
          :before-upload="beforeAvatarUploadBasic"
          :auto-upload="false"
          style="display: inline-block; margin-left: 10px;"
          class="upload-demo"
          action="/api/groupImport">
          <el-button slot="trigger" size="small" type="primary">导入模板</el-button>
          <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUploadBasic">提交模板</el-button>
        </el-upload>
        <el-button type="danger" >
          <el-link style="margin-left: 10px; color: #fff;" icon="el-icon-download" href="/uploads/组长名称.xls" download="" title="组长名称">模板下载</el-link>
        </el-button>
      </el-row>
    </div>
    <br>
    <el-table
      v-loading="listLoading"
      v-if="limits.length >=1 && limits.filter(it => it.FName === '查看').length"
      :key="key"
      :data="tableData"
      max-height="750"
      element-loading-text="正在加载"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
      @current-change="salaryCurrentChange">
      <el-table-column
        type="selection"
        width="55"/>
      <!-- <el-table-column v-for="item in formThead" :key="item.frepindex" :label="item.frepcap" :width="item.frepwid" align="center"> -->
      <el-table-column v-for="item in formThead" :key="item.frepindex" :label="item.frepcap" align="center">
        <template slot-scope="scope">
          <template v-if="item.frephide<=0">
            <template v-if="item.frepfld === 'FStatus'">
              <el-tag v-if="Number(scope.row[item.frepfld]) === 1" type="success">已审</el-tag>
              <el-tag v-else type="danger">未审</el-tag>
            </template>
            <template v-else>
              {{ scope.row[item.frepfld] }}
            </template>

          </template>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
      v-if="limits.length >=1 && limits.filter(it => it.FName === '查看').length"
      :total="total"
      :page-sizes="pagesizes"
      :page-size="pagesize"
      :current-page.sync="currentPage"
      class="jy-pagination"
      style="text-align: center; margin-top: 20px;"
      background
      layout="sizes, prev, pager, next, total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"/>

    <el-dialog v-el-drag-dialog :visible.sync="addUserModal" title="添加／编辑">
      <el-form v-loading="loadingForm" ref="form" :rules="rules" :model="form" label-width="140px">
        <el-form-item v-for="item in dialogThead" :key="item.frepindex" :label="item.frepcap" :prop="item.frepfld">
          <!-- 年份 -->
          <!-- <template v-if="item.frepfld === 'FYear'">
            <el-date-picker
              v-model="form[item.frepfld]"
              style="width: 100%;"
              type="year"
              value-format="yyyy"
              required
              placeholder="选择年度"/>
          </template> -->
          <!-- 下拉框 -->
          <template v-if="item.frepmeg == 1">
            <!-- <template v-if="item.frepfld === 'FPeriod'">
              <el-select v-model="form[item.frepfld]" clearable filterable placeholder="请选择" style="width: 100%;">
                <el-option
                  v-for="item in periodList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </template> -->
            <!-- <template v-else> -->
            <template v-if="item.frepfld === 'fparentname'">
              <el-select v-model="form[item.frepfld]" clearable filterable placeholder="请选择组长" style="width: 100%;">
                <el-option
                  v-for="item in appUserList"
                  :key="item.id"
                  :label="item.username"
                  :value="item.username"/>
              </el-select>
            </template>
            <template v-else-if="item.frepfld === 'fname'">
              <el-select v-model="form[item.frepfld]" clearable filterable placeholder="请选择" style="width: 100%;" @change="changeNickName">
                <el-option
                  v-for="item in nickNameList"
                  :key="item.value"
                  :label="item.label"
                  :disabled="item.disabled"
                  :value="item.label"/>
              </el-select>
            </template>
          </template>
          <!-- 输入框 -->
          <template v-else>
            <el-input v-model="form[item.frepfld]" />
          </template>

        </el-form-item>

        <el-form-item>
          <el-button :loading="loadingForm" type="primary" @click="confirm" >确认</el-button>
          <el-button @click="addUserModal=false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 参数配置 -->
    <el-dialog v-el-drag-dialog :visible.sync="configModal" :close-on-click-modal="false" class="config-dialog" title="表格字段">
      <head-table
        :config-list="configList"
        :key-value="key"
        @updateConfigData="getUpdateConfigData" />
    </el-dialog>

    <!-- SQL弹窗 -->
    <el-dialog v-el-drag-dialog :visible.sync="dialogSQLModal" :append-to-body="true" title="执行结果">
      <el-form v-loading="loadingFormSQL" ref="formSQL" :rules="rulesSQL" :model="formSQL" label-width="120px">
        <el-form-item label="SQL语句" prop="gzsql">
          <el-input v-model="formSQL.gzsql" :autosize="{ minRows: 14 }" type="textarea"/>
        </el-form-item>
        <el-form-item>
          <el-button :loading="loadingFormSQL" type="primary" @click="confirmSQL" >确认</el-button>
          <el-button @click="dialogSQLModal=false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>
<script>
let FRepKey = ''
const defaultFormThead = []
const defaultOptions = [
  {
    value: 0,
    label: '单据设置'
  },
  {
    value: 1,
    label: '显示SQL语句'
  }
]
import { download } from '../../mixins/download.js'
import { common } from '../../mixins/common'
import { company } from '../../mixins/company'
import { pagination } from '../../mixins/pagination'
import { configColumn, erpZhiyuan, checkStatus, AppUser } from '../../api/salary'
import { genAdd, genDel, genList } from '../../api/gen'
import HeadTable from '../salary/headTable/index.vue'
import { tableConfig, apiSql, apiFileSave } from '../../api/account'
import { periodList, getYearMonth } from '../../utils/index'
export default {
  name: 'Index',
  components: {
    HeadTable
  },
  mixins: [download, common, company, pagination],
  data() {
    return {
      // 弹窗下拉列表
      nickNameList: [], // 职员姓名
      periodList: [],

      value: '',
      options: [],
      FRepKey: '',
      // qjValue: getYearMonth(),
      keyword: '',
      // 表头、表格设置
      tableData: [],
      tableList: [],
      defaultFormThead: [],
      formThead: defaultFormThead,
      dialogThead: [],
      curData: {},

      listLoading: false,
      list: [],

      addUserModal: false,
      loadingForm: false,
      isEditor: false,
      form: {},
      rules: {
        FYear: [
          { required: true, message: '请选择年度', trigger: 'change' }
        ],
        appLogin: [
          { required: true, message: '请选择组长', trigger: 'change' }
        ],
        FName: [
          { required: true, message: '请选择职员姓名', trigger: 'change' }
        ]
      },
      // 参数配置
      configModal: false,
      configLoading: false,
      configList: [],
      key: 0,

      // SQL弹窗
      dialogSQLModal: false,
      loadingFormSQL: false,
      formSQL: {
        gzsql: ''
      },
      rulesSQL: {

      },

      fileList: [],

      upData: {},

      // 审核数据
      checkData: [],
      selectedData: [],
      appUserList: [],
      appUser: '',
      appname: '',

      // 用户菜单
      menusList: [],
      limits: [],
      userKey: '',

      fileListBasic: [],
      upDataBasic: {}
    }
  },
  async mounted() {
    this.upData.db = this.globalDb.db
    this.upDataBasic.db = this.globalDb.db
    // this.menusList = this.globalDb.menusList
    this.userKey = this.$route.meta.FRepKey
    FRepKey = this.$route.meta.FRepKey
    this.options = defaultOptions
    this.periodList = periodList
    await this.getAppUser()
    this.getErpZhiyuan()
    // await this.getBodyData()
    // await this.getHeadData()
    await this.getAllData()
    // await this.getExportData()
  },
  methods: {
    // -----------------上传开始---------------------
    beforeAvatarUpload(file) {
      // console.log(file, 9090)
      var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
      const extension = testmsg === 'xls' || testmsg === 'xlsx'
      const isLt2M = file.size / 1024 / 1024 < 1
      if (!extension) {
        this.$message({
          message: '上传文件只能是 xls/xlsx格式!',
          type: 'warning'
        })
      }
      if (!isLt2M) {
        this.$message({
          message: '上传文件大小不能超过 1MB!',
          type: 'warning'
        })
      }

      const fd = new FormData()
      fd.append('file', file)

      console.log(fd)

      if (extension) {
        return true
      } else {
        this.$message.warning('只能上传excel文件格式xls/xlsx')
        return false
      }
      // return extension  && isLt2M
    },

    // async submitUpload() {
    //   this.$refs.upload.submit()
    // },

    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    },
    exceed() {
      // 重复选择文件,弹出错误弹窗
      this.$message('最多仅支持上传一个文件')
    },
    notice(e) {
      console.log(e)
      if (e.code === 200) {
        this.$message(e.Message)
      } else {
        this.$message(e.Message)
      }
    },
    // -----------------上传结束---------------------
    // -----------------上传模板开始---------------------
    beforeAvatarUploadBasic(file) {
      console.log(file, 9090)
      var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
      const extension = testmsg === 'xls' || testmsg === 'xlsx'
      const isLt2M = file.size / 1024 / 1024 < 1
      if (!extension) {
        this.$message({
          message: '上传文件只能是 xls/xlsx格式!',
          type: 'warning'
        })
      }
      if (!isLt2M) {
        this.$message({
          message: '上传文件大小不能超过 1MB!',
          type: 'warning'
        })
      }

      const fd = new FormData()
      fd.append('file', file)

      console.log(fd)

      if (extension) {
        return true
      } else {
        this.$message.warning('只能上传excel文件格式xls/xlsx')
        return false
      }
      // return extension  && isLt2M
    },

    async submitUploadBasic() {
      console.log(this.$refs, ' this.$refs')
      this.$refs.uploadBasic.submit()
    },

    handleRemoveBasic(file, fileList) {
      console.log(file, fileList)
    },
    handlePreviewBasic(file) {
      console.log(file)
    },
    exceedBasic() {
      // 重复选择文件,弹出错误弹窗
      this.$message('最多仅支持上传一个文件')
    },
    noticeBasic(e) {
      console.log(e)
      if (e.code === 200) {
        this.$message(e.message)
      } else {
        this.$message(e.message)
      }
    },
    // -----------------上传模板结束---------------------
    // 获取所有数据 (需要导出的数据)
    // async getExportData() {
    async getExportData(total) {
      console.log(total, ' total 导出')
      // if (!this.total) return
      if (!total) return
      try {
        const res = await genList({
          db: this.globalDb.db,
          opername: decodeURIComponent(this.globalDb.fname),
          FRepKey,
          keyword: this.keyword,
          // qjValue: this.qjValue,
          // limit: this.total,
          limit: total,
          page: this.currentPage
        })
        this.listData = res.Data
        this.formThead.map(item => {
          this.listData.map(it => {
            if (item.freptype === 131) {
              it[item.frepfld] = Number(it[item.frepfld]).toFixed(item.fdecplaces)
            }
          })
        })
      } catch (error) {
        console.log(error)
      }
    },
    // 导出数据
    downData() {
      this.exportData()
    },
    async getAllData() {
      await this.getBodyData()
      await this.getHeadData()
      // 菜单列表（权限）
      // const menuRes = await this.$store.dispatch('GETMENUSLIST', {
      //   db: this.globalDb.db,
      //   loginname: decodeURIComponent(this.globalDb.fname)
      // })
      const menuRes = JSON.parse(localStorage.getItem('setMenuData'))
      // console.log(menuRes, ' menuRes')
      this.menusList = menuRes
      if (menuRes.length >= 1) {
        menuRes.map(it => {
          it.children.map(item => {
            if (item.fobjectid === this.userKey) {
              this.$set(this, 'limits', item.child)
            }
          })
        })
        // console.log(this.limits, '  this.limits')
      }
    },
    // 人员姓名
    async getErpZhiyuan() {
      try {
        this.listLoading = true
        const res = await erpZhiyuan({
          db: this.globalDb.db,
          opername: decodeURIComponent(this.globalDb.fname),
          limit: 10000
        })
        this.listLoading = false
        console.log(res.result, 111111)
        const filterRes = res.result.filter(it => Number(it.fdeleted) !== 1)
        filterRes.map(item => {
          this.nickNameList.push({
            value: item.fnumber,
            label: item.fname + '(' + item.fnumber + ')'
          })
        })
      } catch (error) {
        this.listLoading = false
        console.log(error)
      }
    },
    selectSettings(val) {
      console.log(val, ' val SQL')
      if (val === 0) {
        this.settings()
      } else if (val === 1) {
        this.dialogSQL()
      }
    },
    salaryCurrentChange(val) {
      this.curData = val
    },
    async getTableConfig() {
      try {
        this.configLoading = true
        const res = await tableConfig({
          db: this.globalDb.db,
          opername: decodeURIComponent(this.globalDb.fname),
          key: FRepKey
        })
        this.configList = res.Data
        this.key = FRepKey
        this.configLoading = false
      } catch (error) {
        this.configLoading = false
        console.log(error)
      }
    },
    settings() {
      this.configModal = true
      this.getTableConfig()
    },
    // 更新数据
    async getUpdateConfigData() {
      await this.getTableConfig()
      await this.getBodyData()
      await this.getHeadData()
    },
    // 表头数据
    async getHeadData() {
      try {
        this.listLoading = true
        this.header = []
        this.headerKeys = []
        this.listData = []
        const res = await configColumn({
          db: this.globalDb.db,
          opername: decodeURIComponent(this.globalDb.fname),
          FRepKey
        })
        this.listLoading = false
        this.formThead = res.Data
        this.formThead.map(item => {
          this.tableList.map(it => {
            if (item.freptype === 131) {
              it[item.frepfld] = Number(it[item.frepfld]).toFixed(item.fdecplaces)
            }
          })
        })
        this.tableData = this.tableList.slice()

        // 获取显示表头键值对
        this.formThead.filter(item => {
          if (!item.frephide) {
            this.header.push(item.frepcap)
            this.headerKeys.push(item.frepfld)
          }
        })
        // 获取显示表体数据
        const listData = this.tableList.slice()
        listData.map((item, index) => {
          this.listData[index] = {}
          this.headerKeys.map(it => {
            this.listData[index][it] = item[it]
          })
        })

        // 去除内码
        const arr = res.Data.slice()
        arr.map((item, index) => {
          if (item.frepfld === 'FItemID') {
            arr.splice(index, 1)
          }
        })

        this.dialogThead = arr.filter(item => item.funcontrol !== 1)
        // console.log(this.dialogThead, '  this.dialogThead')
      } catch (error) {
        this.listLoading = false
        console.log(error)
      }
    },
    // 表体数据
    async getBodyData() {
      try {
        this.listLoading = true
        const res = await genList({
          db: this.globalDb.db,
          opername: decodeURIComponent(this.globalDb.fname),
          FRepKey,
          keyword: this.keyword,
          // qjValue: this.qjValue,
          // fcompany: this.fcompany,
          limit: this.pagesize,
          page: this.currentPage
        })
        this.listLoading = false
        this.tableList = res.Data
        this.total = res.total
        // console.log(res, ' res')
        await this.getExportData(this.total)
      } catch (error) {
        this.listLoading = false
        console.log(error)
      }
    },
    // selectPeriod(val) {
    //   this.qjValue = getYearMonth(val)
    //   console.log(this.qjValue, '  this.qjValue')
    //   this.searchUser()
    // },
    async searchUser() {
      // await this.getBodyData()
      // await this.getHeadData()
      await this.getAllData()
    },
    async addUser() {
      this.form = {}
      const arr = getYearMonth().split('-')
      const FYear = arr[0]
      const FPeriod = arr[1] > 10 ? arr[1] : Number(arr[1])

      const data = {}
      this.dialogThead.map(item => { data[item.frepfld] = '' })
      for (const i in data) {
        this.$set(this.form, i, data[i])
      }
      delete this.form.FItemID
      this.form.FRepKey = FRepKey
      this.form.fyear = FYear
      this.form.fperiod = FPeriod

      console.log(this.form, ' addUser this.form')

      setTimeout(() => {
        this.$refs['form'].clearValidate()
      })

      this.addUserModal = true
    },
    changeNickName(val) {
      console.log(val, ' val')
      if (val) {
        const res = this.nickNameList.filter(it => it.label === val)
        this.form.fnumber = res[0].value
      }
    },
    async confirm() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          this.loadingForm = true
          try {
            console.log(this.form, ' confirm this.form')
            this.form.db = this.globalDb.db
            this.form.opername = decodeURIComponent(this.globalDb.fname)
            this.$set(this.form, 'fname', this.form.fname.split('(')[0])
            const res = await genAdd(this.form)
            if (res.Error === 0) {
              this.loadingForm = false
              this.addUserModal = false
              // console.log(res, ' res add')
              this.$message({
                message: res.Message,
                type: 'success'
              })
              // await this.getBodyData()
              // await this.getHeadData()
              await this.getAllData()
            } else {
              this.loadingForm = false
              // this.addUserModal = false
              this.$message({
                message: res.Message,
                type: 'error'
              })
            }
          } catch (error) {
            this.loadingForm = false
            console.log(error)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async editUser() {
      this.form = {}
      console.log(this.selectedData, '  this.selectedData')
      if (this.selectedData.length !== 1) {
        this.$message({
          message: '请选中要编辑的一行',
          type: 'warning'
        })
        return
      }

      // const data = this.curData
      const data = this.selectedData[0]
      console.log(data, ' data edit')

      this.dialogThead.map(item => { this.$set(this.form, item.frepfld, '') })
      this.form.fid = ''
      // this.form.FItemID = ''

      console.log(this.form, ' this.form edit测试')
      // if (!data || !data.fid) {
      //   this.$message({
      //     message: '请选中要编辑的一行',
      //     type: 'warning'
      //   })
      //   return
      // }
      this.addUserModal = true
      for (const i in this.form) {
        this.form[i] = data[i]
      }
      // console.log('data：', data)
      console.log('this.form edit1：', this.form)
      this.form.FRepKey = FRepKey
      this.form.fyear = 2021
      const label = this.nickNameList.filter(item => item.label.split('(')[0] === this.form.fname)[0].label
      console.log(label, ' label')
      this.$set(this.form, 'fname', label)
      setTimeout(() => {
        this.$refs['form'].clearValidate()
      })
    },
    async delUser() {
      // const data = this.curData
      const data = this.selectedData
      const isFstatus = data.filter(it => Number(it.FStatus) === 1)
      const fid = data.map(it => it.fid).join(',')
      // console.log(fid, '  fid  ')

      // console.log(isFstatus, ' isFstatus')
      if (isFstatus.length) {
        this.$message({
          message: '已审核，不能删除！',
          type: 'warning'
        })
        return
      }
      if (!this.checkData.length) {
        this.$message({
          message: '请选中要删除的行',
          type: 'warning'
        })
        return
      }

      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'btn-custom-cancel',
        type: 'warning'
      }).then(async() => {
        // console.log(data, ' data')
        // const { fid } = data
        const res = await genDel({
          db: this.globalDb.db,
          opername: decodeURIComponent(this.globalDb.fname),
          fid,
          FRepKey
        })
        if (res.Error === 0) {
          this.$message({
            message: res.Message,
            type: 'success'
          })
          // await this.getBodyData()
          // await this.getHeadData()
          await this.getAllData()
          return
        }
        this.$message({
          message: res.Message,
          type: 'error'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async handleSizeChange(val) {
      this.currentPage = 1
      this.pagesize = val
      // await this.getBodyData()
      // await this.getHeadData()
      await this.getAllData()
    },
    async handleCurrentChange(val) {
      this.currentPage = val
      // await this.getBodyData()
      // await this.getHeadData()
      await this.getAllData()
    },
    // SQL修改/更新
    async dialogSQL() {
      this.dialogSQLModal = true
      try {
        const res = await apiSql({
          db: this.globalDb.db,
          opername: decodeURIComponent(this.globalDb.fname),
          FRepKey
        })
        // console.log(res, ' res')
        if (res.Error === 0) {
          this.formSQL.gzsql = res.Data
          this.formSQL.FRepKey = FRepKey
        }
        setTimeout(() => {
          this.$refs['formSQL'].clearValidate()
        })
      } catch (error) {
        console.log(error)
      }
    },
    confirmSQL() {
      this.$refs['formSQL'].validate(async(valid) => {
        if (valid) {
          try {
            this.formSQL.db = this.globalDb.db
            this.formSQL.opername = decodeURIComponent(this.globalDb.fname)
            const res = await apiFileSave(this.formSQL)
            if (res.Error === 0) {
              this.dialogSQLModal = false
              this.$message({
                message: res.Message,
                type: 'success'
              })
            } else {
              this.$message({
                message: res.Message,
                type: 'error'
              })
            }
          } catch (error) {
            console.log(error)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleSelectionChange(val) {
      this.checkData = []
      this.selectedData = val
      // console.log(this.selectedData, '  this.selectedData')
      console.log(val, ' val ')
      val.map(it => {
        this.checkData.push(it.fid)
      })
      console.log(this.checkData.join(','), "  this.checkData.join(', ')")
      // this.checkData = val
    },
    async getAppUser() {
      try {
        const res = await AppUser({
          opername: decodeURIComponent(this.globalDb.fname),
          db: this.globalDb.db
        })
        console.log(res.Data, '  res.Data')
        this.appUserList = res.Data
      } catch (error) {
        console.log(error)
      }
    },
    async check(Fstatus) {
      // if (!this.appUser) {
      //   this.$message({
      //     message: '请选择员工',
      //     type: 'error'
      //   })
      //   return
      // }
      if (!this.checkData.length) {
        this.$message({
          message: '请选择表格行数据',
          type: 'error'
        })
        return
      }
      if (Number(Fstatus) === 1) {
        this.$confirm('是否审核, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          cancelButtonClass: 'btn-custom-cancel',
          type: 'warning'
        }).then(async() => {
          try {
            const res = await checkStatus({
              db: this.globalDb.db,
              opername: decodeURIComponent(this.globalDb.fname),
              loginname: decodeURIComponent(this.globalDb.fname),
              FRepKey,
              Fstatus, // 1—审核，0—反审
              fid: this.checkData.join(','),
              appname: ''
            })
            if (res.Error === 0) {
              this.$message({
                message: res.Message,
                type: 'success'
              })
              await this.getAllData()
            } else {
              this.loadingForm = false
              this.$message({
                message: res.Message,
                type: 'error'
              })
            }
          } catch (error) {
            console.log(error)
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      } else {
        try {
          const res = await checkStatus({
            db: this.globalDb.db,
            opername: decodeURIComponent(this.globalDb.fname),
            loginname: decodeURIComponent(this.globalDb.fname),
            FRepKey,
            Fstatus, // 1—审核，0—反审
            fid: this.checkData.join(','),
            appname: ''
          })
          if (res.Error === 0) {
            this.$message({
              message: res.Message,
              type: 'success'
            })
            await this.getAllData()
          } else {
            this.loadingForm = false
            this.$message({
              message: res.Message,
              type: 'error'
            })
          }
        } catch (error) {
          console.log(error)
        }
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.config-dialog ::v-deep .el-dialog {
  width: 90%;
}
  .el-row div, button {
    margin-top: 10px;
  }
</style>
